// pages/add/add.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min');
var qqmapsdk;
const app = getApp()
Page({
  data: {
    title: '',
    complete_date: '',
    address: '',
    url: '',
    path: ''
  },

  onLoad(options) {
    qqmapsdk = new QQMapWX({
      key: 'Z37BZ-4VIWU-RNHV6-4EUZF-PA7R6-NWBX2'
    });
  },
  setTitle: function (option) {
    this.setData({
      title: option.detail.value
    })
  },
  setDate: function (option) {
    this.setData({
      complete_date: option.detail.value
    })
  },
  add: function () {
    wx.request({
      url: app.apiHost + 'task/add',
      data: {
        openid: wx.getStorageSync('openid'),
        title: this.data.title,
        complete_date: this.data.complete_date
      },
      method: 'POST',
      success: function (res) {
        var result = res.data
        if (result.status == 200) {
          wx.navigateBack()
        } else {
          wx.showToast({
            title: result.message,
            icon: 'none'
          })
        }
      }
    })
  },
  getMyLocation: function () {
    var that = this
    wx.getLocation({
      type: 'gjc02',
      success: (result) => {
        console.log(result)
        qqmapsdk.reverseGeocoder({
          location: {
            longitude: result.longitude,
            latitude: result.latitude
          },
          success: function(result) {
          }
        })
      },
      fail: (res) => {
        wx.showModal({
          title: '提示',
          content: '请打开地理位置获取权限',
          complete: (res) => {
            if (res.confirm) {
              wx.openSetting()
            }
          }
        })
      }
    })
  },
  uploadImage: function() {
    var that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sizeType: ['compressed'],
      sourceType: ['camera', 'camera'],
      success: (result) => {
        console.log(result)
        wx.uploadFile({
          filePath: result.tempFiles[0].tempFilePath,
          name: 'sign_name',
          url: app.apiHost + 'task/upload',
          success: (result) => {
            var result = JSON.parse(result.data)
            that.setData({
              url: result.data.url,
              path: result.data.path
            })
          },
        })
      }
    })
  }
})